/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mapper;

import Bean.AccountBean;
import java.sql.*;
import java.util.*;
import java.sql.PreparedStatement;

/**
 *
 * @author Quang Thao
 */
public class AccountMapper extends DBMapper {

    public AccountMapper() throws SQLException, Exception {
        super();
    }

    public boolean IsTrue(Bean.AccountBean acc) throws Exception {
        PreparedStatement stmt = getConnection().prepareStatement("SELECT * FROM Account Where ID='" + acc.getID()
                + "' AND Pass='" + acc.getPass() + "'");
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            stmt.close();
            return true;
        }
        stmt.close();
        return false;
    }

    public boolean IsExist(Bean.AccountBean acc) throws Exception {
        PreparedStatement stmt = getConnection().prepareStatement("SELECT * FROM Account Where ID='" + acc.getID()
                + "'");
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            stmt.close();
            return true;
        }
        stmt.close();
        return false;
    }

    public Bean.AccountBean GetInfo(String id) throws Exception {
        PreparedStatement stmt = getConnection().prepareStatement("SELECT * FROM Account Where ID='" + id + "'");
        ResultSet rs = stmt.executeQuery();
        Bean.AccountBean acc = new Bean.AccountBean();
        if (rs != null && rs.next()) {
            acc.setID(rs.getString(1));
            acc.setPass(rs.getString(2));
            acc.setName(rs.getString(3));
            acc.setEmail(rs.getString(4));
            acc.setPhone(rs.getString(5));
            acc.setAddress(rs.getString(6));
            acc.setType(rs.getString(7));
        }
        stmt.close();
        return acc;
    }

    public List GetAll() throws Exception {
        List list = new ArrayList<Bean.AccountBean>();
        PreparedStatement stmt = getConnection().prepareStatement("SELECT * FROM Account");
        ResultSet rs = stmt.executeQuery();
        if (rs != null) {
            while (rs.next()) {
                Bean.AccountBean acc = new Bean.AccountBean();
                acc.setID(rs.getString(1));
                acc.setPass(rs.getString(2));
                acc.setName(rs.getString(3));
                acc.setEmail(rs.getString(4));
                acc.setPhone(rs.getString(5));
                acc.setAddress(rs.getString(6));
                acc.setType(rs.getString(7));
                list.add(acc);
            }
        }
        stmt.close();
        return list;
    }

    public List GetListByType(String type) throws Exception {
        List list = new ArrayList<Bean.AccountBean>();
        PreparedStatement stmt = getConnection().prepareStatement("SELECT * FROM Account WHERE Type='" + type + "'");
        ResultSet rs = stmt.executeQuery();
        if (rs != null) {
            while (rs.next()) {
                Bean.AccountBean acc = new Bean.AccountBean();
                acc.setID(rs.getString(1));
                acc.setPass(rs.getString(2));
                acc.setName(rs.getString(3));
                acc.setEmail(rs.getString(4));
                acc.setPhone(rs.getString(5));
                acc.setAddress(rs.getString(6));
                acc.setType(rs.getString(7));
                list.add(acc);
            }
        }
        stmt.close();
        return list;
    }

    public int Add(Bean.AccountBean acc) throws Exception {
        PreparedStatement stmt = getConnection().prepareStatement("Insert Into Account Values('"
                + acc.getID() + "','" + acc.getPass() + "','" + acc.getName() + "','"
                + acc.getEmail() + "','" + acc.getAddress() + "','" + acc.getPhone() + "','"
                + acc.getType() + "')");
        int found = stmt.executeUpdate();
        stmt.close();
        return found;
    }

    public int Delete(AccountBean acc) throws Exception {
        PreparedStatement stmt = getConnection().prepareStatement("Delete From Account Where ID='" + acc.getID() + "'");
        int found = stmt.executeUpdate();
        stmt.close();
        return found;
    }
}
